package com.gogo.mall.controller;
import com.gogo.mall.entity.QyAddress;
import com.gogo.mall.entity.QyMember;
import com.gogo.mall.jwt.PassToken;
import com.gogo.mall.result.ResponseCode;
import com.gogo.mall.result.ResponseData;
import com.gogo.mall.service.QyAddressService;
import com.gogo.mall.service.QyMemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("member")
public class QyMemberController {
    @Resource
    private QyMemberService qyMemberService;

    @Autowired
    private QyAddressService qyAddressService;
    //个人中心
    @PostMapping("index")
    @PassToken
    public ResponseData index(HttpServletRequest request){
        QyMember userInfo = (QyMember) request.getAttribute("UserInfo");
        QyMember qyMember = qyMemberService.queryById(userInfo.getId());
        return new ResponseData(ResponseCode.WEB_SUCCESS,qyMember);
    }
    @RequestMapping("test")
    @PassToken
    public String test(@RequestBody  QyMember memberInfo){
        qyMemberService.queryByTel(memberInfo);
        return  "啊好久好久阿基";
    }

    //获取收货地址
    @PostMapping("addressList")
    @PassToken
    public ResponseData address(HttpServletRequest request){
        QyMember userInfo = (QyMember) request.getAttribute("UserInfo");
        List<QyAddress> addrs = qyAddressService.queryByUerId(userInfo.getId());
        return new ResponseData(ResponseCode.SUCCESS,addrs);
    }

    //添加收货地址
    @PostMapping("addressAdd")
    @PassToken
    public ResponseData addressAdd(HttpServletRequest request, @RequestBody QyAddress qyAddress){
        QyMember userInfo = (QyMember) request.getAttribute("UserInfo");
        qyAddress.setUid(userInfo.getId());
        QyAddress insert = qyAddressService.insert(qyAddress);
            if(ObjectUtils.isEmpty(insert))
                return new ResponseData(ResponseCode.FAILED);
        return new ResponseData(ResponseCode.SUCCESS);
    }

    //编辑地址
    @PostMapping("addressEdit")
    @PassToken
    public  ResponseData addressEdit(HttpServletRequest request, @RequestBody QyAddress address){
        QyAddress update = qyAddressService.update(address);
        if(ObjectUtils.isEmpty(update))
            return new ResponseData(ResponseCode.FAILED);
        return new ResponseData(ResponseCode.SUCCESS);
    }

    //根据地址id查询地址
    @PostMapping("getAddressInfo")
    @PassToken
    public  ResponseData getAddressInfo(@RequestBody Map<String,Integer> map){
        QyAddress addr = qyAddressService.queryById(map.get("id"));
        return new ResponseData(ResponseCode.SUCCESS,addr);
    }

    //修改用户昵称
    @PostMapping("setNickname")
    @PassToken
    public ResponseData setNickname(HttpServletRequest request, @RequestBody Map<String,String> map){
        QyMember user = (QyMember) request.getAttribute("UserInfo");
        user.setNickname(map.get("nickname"));
        qyMemberService.update(user);
        return new ResponseData(ResponseCode.SUCCESS);
    }


    //根据地址id删除地址
    @PostMapping("addressDel")
    @PassToken
    public  ResponseData addressDel(@RequestBody Map<String,Integer> map){
        boolean id = qyAddressService.deleteById(map.get("id"));
        if(id)
        return new ResponseData("200","删除成功");
        return new ResponseData(ResponseCode.FAILED);
    }

    //更换头像
    @PostMapping("setHead")
    @PassToken
    public ResponseData setHead(HttpServletRequest request) throws IOException {
        QyMember user = (QyMember) request.getAttribute("UserInfo");
        try {
            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
            //对应前端的upload的name参数"file"
            MultipartFile file = req.getFile("image");
            // 上传并返回新文件名称
            String url = qyMemberService.uploadImg(file);

            user.setImage(url);
            qyMemberService.update(user);
            return new ResponseData(ResponseCode.SUCCESS,url);
        }
        catch (Exception e) {
            return new ResponseData(ResponseCode.FAILED,e.getMessage());
        }
    }
}
